import { getList } from '@/services/goods';
export default {
  state: {
    list: {
      allCount: 0, //总数量
      data: [], // 商品数据
    },
    listParams: {
      searchText: '休闲',
      page: 1,
      everyNum: 10,
    },
  },
  reducers: {
    overrideStateProps(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },

    updateStateProps(state, { payload }) {
      const { name, value } = payload;

      return {
        ...state,
        ...{ [name]: { ...state[name], ...value } },
      };
    },
  },
  effects: {
    // 请求商品数据
    *getList(_, { call, put, select }) {
      // call, put, select 前面必须写yield
      const { listParams } = yield select((state) => state.goods);
      const { data } = yield call(getList, listParams);

      yield put({
        type: 'overrideStateProps',
        payload: {
          list: data.list,
        },
      });
    },
    *reset(_, { put }) {
      // 第一步 重置条件

      yield put({
        type: 'overrideStateProps',
        payload: {
          listParams: {
            searchText: '休闲',
            page: 1,
            everyNum: 10,
          },
        },
      });
      // 第二步 发请求
      yield put({
        type: 'getList',
      });
    },
  },
};
